//
//  VCInformationTool.m
//  VCDemo
//
//  Created by 肖康龙 on 2017/9/5.
//  Copyright © 2017年 vsoontech. All rights reserved.
//

#import "VCInformationTool.h"
#import "VCDowmload.h"


@implementation VCInformationTool
static NSDictionary *VCDict_file;
static NSMutableArray *VC_arrayTsNum;//每一个ts片段谁下载下来的
static NSMutableArray *VC_arrayTs;
static NSInteger VC_sequence;//m3u8序列号
static NSDictionary *VC_keyDict;//加解密
static NSMutableDictionary *VC_dictPath;//存在的路径
static BOOL VC_isChangeTime;//是否在更换时间
static NSFileManager *VC_fileManager;//文件句柄
static BOOL VC_isLastTs;//当前视频是否获取到最后一个ts
static BOOL VC_isRestart;//播放器是否重启了

/**获取key*/
+(NSDictionary *)getKeyDict{
    return VC_keyDict;
}
/**设置key*/
+(void)setKeyDict:(NSDictionary *)keyDict{
    VC_keyDict = keyDict;
}


/**播放器是否重启了*/
+(BOOL)getPlayerIsRestart{
    return VC_isRestart;
}
/**设置播放器是否重启了*/
+(void)setPlayerIsRestart:(BOOL)isRestart{
    if (isRestart && VC_sequence > 0) {//当VC_sequence大于0并且播放器重启了,就sequece添加一
        VC_sequence++;
    }
    VC_isRestart = isRestart;
}

/**获取当前视频是否获取到最后一个ts*/
+(BOOL)getVideoIsLastTs{
    return VC_isLastTs;
}
/**设置当前视频是否获取到最后一个ts*/
+(void)setVideoIsLastTs:(BOOL)isLastTs{
    VC_isLastTs = isLastTs;
}

/**获取当前播放的ts号*/
+(NSInteger)getVC_sequence{
    return VC_sequence;
}
/**设置当前下一个需要播放ts号*/
+(void)setVC_sequence:(NSInteger)sequence{
    VC_sequence = sequence;
}
/**是否在切换时间*/
+(BOOL)getPlayerIsChangeTime{
    return VC_isChangeTime;
}
/**设置是否在更换时间
 *yes 正在更换 no不是在更换
 */
+(void)setPlayerChangeTime:(BOOL)isChange{
    VC_isChangeTime = isChange;
}

/**感觉tsNum检查ts文件是否存在*/
+(BOOL)checkTSFileIsHaveWithTsNum:(NSInteger)tsNum{
    if (VC_fileManager == nil) {
        VC_fileManager = [NSFileManager defaultManager];
    }
    return [VC_fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/%ld.ts",kVideoSavePath,tsNum] isDirectory:nil];
}

/**检查是否存在路径
 *path      路径
 *return    存在返回yes 不存在会创建,创建成功返回yes 创建失败返回no
 */
+(BOOL)checkHaveWithPath:(NSString *)path{
    if (path == nil && [path isKindOfClass:[NSString class]]) {//需要字符串路径
        return NO;
    }
    if (VC_dictPath == nil) {
        VC_dictPath = [NSMutableDictionary dictionary];
    }
    NSNumber *isHave = VC_dictPath[path];
    if (isHave == nil) {
        if (VC_fileManager == nil) {
            VC_fileManager = [NSFileManager defaultManager];
        }
        if ([VC_fileManager fileExistsAtPath:path isDirectory:nil]) {//存在路径
            [VC_dictPath setObject:@(1) forKey:path];
            return YES;
        }else{//不存在创建
            //不存在这个链接
            BOOL isS = [VC_fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
            if (isS) {//创建成功
                return YES;
            } else {//创建失败
                return NO;
            }
        }
    }else{//存在路径
        return YES;
    }
    
}


/*拼接ts的list列表*/
+(NSDictionary *)appendTsListWithSource:(NSDictionary *)sourceList addList:(NSDictionary *)addList{
    if (sourceList == nil) {
        return addList;
    }else{
        NSMutableDictionary *dict = [sourceList mutableCopy];//复制出来的列表信息
        NSMutableArray *tsList = [[sourceList objectForKey:@"fileList"] mutableCopy];//原来的ts列表
        
        NSArray *array = [addList objectForKey:@"fileList"];//新的ts列表
        
        if (array.count > 0) {
            NSDictionary *oneTs = array[0];
            
            NSInteger index = 0;
            for (NSInteger i = 0; i < tsList.count; i++) {//寻找新的ts列表开始位置在哪里
                NSDictionary *oldTs = tsList[i];
                if ([[oldTs objectForKey:@"fileId"] isEqualToString:[oneTs objectForKey:@"fileId"]]) {
                    index = i;
                }
            }
            
            NSMutableArray *remoreList = [NSMutableArray array];
            for (NSInteger i = index; i < tsList.count; i++) {
                [remoreList addObject:tsList[i]];
            }
            [tsList removeObjectsInArray:remoreList];//去除重复的ts
            [tsList addObjectsFromArray:array];//添加新的ts
            
            [dict setObject:tsList forKey:@"fileList"];//替换ts列表
            [dict setObject:sourceList[@"endList"] forKey:@"endList"];//是否结束
//            [dict setObject:sourceList[@"firstSec"] forKey:@"firstSec"];//替换开始的秒数
//            [dict setObject:@(index) forKey:@"seq"];//最新的一片开始开始的位置

            
            return dict;//替换列表信息
        }
        return sourceList;
        
    }
}

/**保存信息*/
+(void)saveFileDict:(NSDictionary *)dict{
    VCDict_file = dict;
}
/**获取列表信息*/
+(NSDictionary *)getFileDict{
    return VCDict_file;
}
/**保存当前缓存的ts列表
 *tsName
 *isReset 是否重置
 *tsNum 谁下载完毕的
 */
+(void)saveTSFileWithTsName:(NSString *)tsName isReset:(BOOL)isReset tsNum:(NSUInteger)tsNum{
    if (VC_arrayTs == nil) {
        VC_arrayTs = [NSMutableArray array];
        VC_arrayTsNum = [NSMutableArray array];
    }
    if (isReset) {//清零
        [VC_arrayTs removeAllObjects];
        [VC_arrayTsNum removeAllObjects];
    }else{
        [VC_arrayTs addObject:tsName];
        [VC_arrayTsNum addObject:@(tsNum)];
    }
    
}
/**获取当前缓存的ts列表*/
+(NSArray *)getTSFileArray{
    return  VC_arrayTs;
}

/**获取当前缓存缓存是哪个下载完成的
 *index 第几个ts
 *fileName tsid
 */
+(NSUInteger)getTSDownloadNumWithIndex:(NSUInteger)index withFileName:(NSString *)fileName{
    if (fileName != nil) {
        NSArray *array = VC_arrayTs;
        for (int i = 0; i < array.count; i++) {
            NSString *path = array[i];
            if ([path containsString:fileName]) {
                if (i < VC_arrayTsNum.count) {
                    return [VC_arrayTsNum[i] unsignedIntegerValue];
                }else{
                    return 0;
                }
            }
        }
        return 0;
    }else{
        return 0;
    }
}

// 删除沙盒里的文件
+(void)deleteFileWithPath:(NSString *)path {
    if (VC_fileManager == nil) {
        VC_fileManager = [NSFileManager defaultManager];
    }
    
    BOOL blHave=[VC_fileManager fileExistsAtPath:path];
    if (!blHave) {
        VCNSLog(@"no  have");
        return ;
    }else {
        VCNSLog(@" have");
        BOOL blDele= [VC_fileManager removeItemAtPath:path error:nil];
        if (blDele) {
            [VC_dictPath removeObjectForKey:path];
            
            VCNSLog(@"dele success path = %@",path);
        }else {
            VCNSLog(@"dele fail");
        }
        
    }
}


/**
 *  总的空间
 */
+ (CGFloat )totalDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    
    return [[fattributes objectForKey:NSFileSystemSize] floatValue];
}

/**
 *  剩余空间
 */
+ (CGFloat )freeDiskSpace
{
    NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    
    return [[fattributes objectForKey:NSFileSystemFreeSize] floatValue];
}

/**
 *  已用空间
 */
+ (CGFloat )haveUseDiskSpace
{
    CGFloat totalDiskSpace = [self totalDiskSpace];
    CGFloat freeDiskSpace = [self freeDiskSpace];
    CGFloat haveUseDiskSpace = totalDiskSpace - freeDiskSpace;
    
    return [[NSNumber numberWithFloat:haveUseDiskSpace] floatValue];
}



@end
